package com.aiti.lulian.utils;

import cn.hutool.core.util.StrUtil;

public class MyStrUtil {
    /**
     * 由驼峰形式的字符串获得下划线形式的字符串
     * @param str
     * @return
     */
    public static String camelToUnderline(String str) {
        if (str == null || "".equals(str.trim())) {
            return "";
        }
        int len = str.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = str.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append("_").append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        String strUp = sb.toString().toUpperCase();
        return strUp;
    }

    /**
     * 提取文件id
     * @param url
     * @return
     */
    public static String getFileIdByUrl(String url) {
        if(StrUtil.isEmpty(url)) {
            return null;
        }
        return url.substring(url.lastIndexOf("/") + 1);
    }

    /**
     * 提取视频id
     * @param url https://yunzhanting.org.cn/video/879355ff-bc1c-47ed-a561-1e541a086ce5.mp4
     * @return 879355ff-bc1c-47ed-a561-1e541a086ce5
     */
    public static String getVideoIdByUrl(String url) {
        if(StrUtil.isEmpty(url)) {
            return null;
        }
        int lastSlashIndex = url.lastIndexOf('/');
        int dotMp4Index = url.indexOf(".", lastSlashIndex);

        if (lastSlashIndex != -1 && dotMp4Index != -1) {
            return url.substring(lastSlashIndex + 1, dotMp4Index);
        }
        return null;
    }
}